package com.turnengine.client.local.location.command.set;

import static com.javabi.common.dependency.DependencyFactory.getDependency;

import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.errorcode.ErrorCodeException;
import com.javabi.command.executor.ICommandExecutorService;
import com.turnengine.client.local.location.bean.ICoordinateGrid;
import com.turnengine.client.local.location.bean.IGridDetails;
import com.turnengine.client.local.location.bean.ILocation;
import com.turnengine.client.local.location.command.GenerateLocations;
import com.turnengine.client.local.location.command.GetCoordinateGrids;
import com.turnengine.client.local.location.command.GetGridDetailsList;
import com.turnengine.client.local.location.command.GetLocationList;
import com.turnengine.client.local.location.command.GetLocationSignupsEnabled;
import com.turnengine.client.local.location.command.GetLocationsEnabled;
import com.turnengine.client.local.location.command.IGenerateLocations;
import com.turnengine.client.local.location.command.IGetCoordinateGrids;
import com.turnengine.client.local.location.command.IGetGridDetailsList;
import com.turnengine.client.local.location.command.IGetLocationList;
import com.turnengine.client.local.location.command.IGetLocationSignupsEnabled;
import com.turnengine.client.local.location.command.IGetLocationsEnabled;
import com.turnengine.client.local.location.command.ISetCoordinateGridPlugin;
import com.turnengine.client.local.location.command.ISetLocationCoordinatePlugin;
import com.turnengine.client.local.location.command.ISetLocationGenerationPlugin;
import com.turnengine.client.local.location.command.ISetLocationSignupsEnabled;
import com.turnengine.client.local.location.command.ISetLocationsEnabled;
import com.turnengine.client.local.location.command.SetCoordinateGridPlugin;
import com.turnengine.client.local.location.command.SetLocationCoordinatePlugin;
import com.turnengine.client.local.location.command.SetLocationGenerationPlugin;
import com.turnengine.client.local.location.command.SetLocationSignupsEnabled;
import com.turnengine.client.local.location.command.SetLocationsEnabled;
import java.util.List;

/**
 * The Local Location Command Set.
 */
public class LocalLocationCommandSet implements ILocalLocationCommandSet {

	@Override
	public Boolean setLocationGenerationPlugin(long loginId, int instanceId, String plugin) {
		ISetLocationGenerationPlugin command = new SetLocationGenerationPlugin(loginId, instanceId, plugin);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Integer generateLocations(long loginId, int instanceId) {
		IGenerateLocations command = new GenerateLocations(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean setCoordinateGridPlugin(long loginId, int instanceId, String plugin) {
		ISetCoordinateGridPlugin command = new SetCoordinateGridPlugin(loginId, instanceId, plugin);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<ICoordinateGrid> getCoordinateGrids(long loginId, int instanceId) {
		IGetCoordinateGrids command = new GetCoordinateGrids(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<ICoordinateGrid>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean setLocationCoordinatePlugin(long loginId, int instanceId, String plugin) {
		ISetLocationCoordinatePlugin command = new SetLocationCoordinatePlugin(loginId, instanceId, plugin);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean setLocationsEnabled(long loginId, int instanceId, boolean enabled) {
		ISetLocationsEnabled command = new SetLocationsEnabled(loginId, instanceId, enabled);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean getLocationsEnabled(long loginId, int instanceId) {
		IGetLocationsEnabled command = new GetLocationsEnabled(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean setLocationSignupsEnabled(long loginId, int instanceId, boolean enabled) {
		ISetLocationSignupsEnabled command = new SetLocationSignupsEnabled(loginId, instanceId, enabled);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean getLocationSignupsEnabled(long loginId, int instanceId) {
		IGetLocationSignupsEnabled command = new GetLocationSignupsEnabled(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<ILocation> getLocationList(long loginId, int instanceId) {
		IGetLocationList command = new GetLocationList(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<ILocation>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IGridDetails> getGridDetailsList(long loginId, int instanceId, int[] coordinates) {
		IGetGridDetailsList command = new GetGridDetailsList(loginId, instanceId, coordinates);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IGridDetails>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}
}
